package activity.addCamera;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.hichip.campro.R;
import java.util.ArrayList;
import base.HiActivity;
import bean.MyCamera;
import butterknife.BindView;
import common.HiDataValue;
import common.TitleView;
import custom.dialog.Effectstype;
import custom.dialog.WarningDialogBuilder;
import main.MainActivity;
import utils.HiTools;

public class TakeNameToCamActivity extends HiActivity implements View.OnClickListener {

    @BindView(R.id.title) TitleView title;
    @BindView(R.id.tv_one) TextView tv_one;
    @BindView(R.id.tv_two) TextView tv_two;
    @BindView(R.id.tv_there) TextView tv_there;
    @BindView(R.id.tv_four) TextView tv_four;
    @BindView(R.id.tv_five) TextView tv_five;
    @BindView(R.id.tv_six) TextView tv_six;
    @BindView(R.id.tv_seven) TextView tv_seven;
    @BindView(R.id.tv_eight) TextView tv_eight;

    private ArrayList<TextView> listTv = new ArrayList<>();
    @BindView(R.id.et_name) EditText et_name;
    @BindView(R.id.tv_confirm) TextView tv_confirm;
    private MyCamera mMyCamera;



    @Override
    protected int setLayoutId() {
        return R.layout.activity_takename_tocam;
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        getIntentData();
        initViewAndData();
        setListeners();

    }

    private void getIntentData() {
        String str_uid = getIntent().getStringExtra(HiDataValue.EXTRAS_KEY_UID);
        if (!TextUtils.isEmpty(str_uid)) {
            for (MyCamera camera : HiDataValue.CameraList) {
                if (str_uid.equals(camera.getUid())) {
                    this.mMyCamera = camera;
                    break;
                }
            }
        }
        if(HiTools.cameraWhetherNull(mMyCamera,this)) return;
    }

    private void setListeners() {
        for (TextView tv : listTv) {
            tv.setOnClickListener(this);
        }
        tv_confirm.setOnClickListener(this);
    }

    private void initViewAndData() {
        title.setTitle(getString(R.string.title_name_setting));
        title.setButton(TitleView.NAVIGATION_BUTTON_LEFT);
        title.setRightTxtBack(R.mipmap.x_bule);
        title.setNavigationBarButtonListener(new TitleView.NavigationBarButtonListener() {
            @Override
            public void OnNavigationButtonClick(int which) {
                switch (which) {
                    case TitleView.NAVIGATION_BUTTON_LEFT:
                        Intent intent = new Intent(TakeNameToCamActivity.this, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        break;
                    case TitleView.NAVIGATION_BUTTON_IV_RIGHT:
                        intent = new Intent(TakeNameToCamActivity.this, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent, true);
                        break;
                }
            }
        });
        listTv.add(tv_one);
        listTv.add(tv_two);
        listTv.add(tv_there);
        listTv.add(tv_four);
        listTv.add(tv_five);
        listTv.add(tv_six);
        listTv.add(tv_seven);
        listTv.add(tv_eight);

        et_name.setSelection(et_name.getText().toString().length());

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_one:
                et_name.setText(getString(R.string.parlour));
                setTxtViewSelect(0);
                et_name.setSelection(et_name.getText().toString().length());
                break;
            case R.id.tv_two:
                et_name.setText(getString(R.string.bedroom));
                setTxtViewSelect(1);
                et_name.setSelection(et_name.getText().toString().length());
                break;
            case R.id.tv_there:
                et_name.setText(getString(R.string.doorway));
                setTxtViewSelect(2);
                et_name.setSelection(et_name.getText().toString().length());
                break;
            case R.id.tv_four:
                et_name.setText(getString(R.string.babyroom));
                setTxtViewSelect(3);
                et_name.setSelection(et_name.getText().toString().length());
                break;
            case R.id.tv_five:
                et_name.setText(getString(R.string.shop));
                setTxtViewSelect(4);
                et_name.setSelection(et_name.getText().toString().length());
                break;
            case R.id.tv_six:
                et_name.setText(getString(R.string.corridor));
                setTxtViewSelect(5);
                et_name.setSelection(et_name.getText().toString().length());
                break;
            case R.id.tv_seven:
                et_name.setText(getString(R.string.hall));
                setTxtViewSelect(6);
                et_name.setSelection(et_name.getText().toString().length());
                break;
            case R.id.tv_eight:
                et_name.setText(getString(R.string.office));
                setTxtViewSelect(7);
                et_name.setSelection(et_name.getText().toString().length());
                break;
            case R.id.tv_confirm:
                String str_name = et_name.getText().toString();
                if (TextUtils.isEmpty(str_name)) {
                    showAlert(getString(R.string.device_name_not_empty));
                    return;
                }
                mMyCamera.setNikeName(str_name);
                mMyCamera.updateInDatabase(this);
                Intent intent = new Intent(TakeNameToCamActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                this.finish();
                break;
        }
    }

    private  void setTxtViewSelect(int value){
        for(int i=0;i<listTv.size();i++){
            TextView tv=listTv.get(i);
            tv.setSelected(i==value?true:false);
        }
    }

    public void showAlert(CharSequence msg) {
        WarningDialogBuilder builder = WarningDialogBuilder.getInstance(this);
        builder.withMessage(msg).isCancelable(true).isCancelableOnTouchOutside(true).withEffect(Effectstype.Shake);
        builder.show();
    }
}
